package com.example.controller;

import com.example.common.Result;
import com.example.pojo.User;
import com.example.pojo.user.RolePermission;
import com.example.pojo.user.UserRole;
import com.example.service.RolePermissionService;
import com.example.service.UserRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/permission")
@Api(tags = "用户身份,和身份对应权限综合操作")
@RestController
@Slf4j
public class UserPermissionController {
    @Autowired
    RolePermissionService rolePermissionService;

    @Autowired
    UserRoleService roleService;

    @PostMapping("/deleteRole")
    @ApiOperation("删除用户的身份")
    @RequiresRoles("admin")
    public Result<Integer> deleteRole(@RequestBody UserRole userRole) {
        log.info(userRole.toString());
        return Result.SUCCESS(roleService.deleteUserRole(userRole));
    }

    @PostMapping("/insertRole")
    @ApiOperation("添加用户的身份")
    @RequiresRoles("admin")
    public Result<Integer> insertRole(@RequestBody UserRole user_role) {
        return Result.SUCCESS(roleService.insertSelective(user_role));
    }

    @PostMapping("/selectUserRole")
    @ApiOperation("查看用户身份")
    public Result<List<UserRole>> selectUserRole(@RequestBody UserRole userRole) {
        return Result.SUCCESS(roleService.selectByPrimaryKey(userRole.getUuid()));
    }

    @GetMapping("/selectUserRole2")
    @ApiOperation("查看用户身份2")
    public Result<List<UserRole>> selectUserRole() {
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        return Result.SUCCESS(roleService.selectByPrimaryKey(user.getUuid()));
    }

    @PostMapping("/updateUserRole")
    @ApiOperation("修改用户身份")
    @RequiresRoles("admin")
    public Result<Integer> updateUserRole(@RequestBody UserRole userRole) {
        return Result.SUCCESS(roleService.updateByPrimaryKeySelective(userRole));
    }
    @GetMapping("/listRole")
    @ApiOperation("列出所有身份")
    public Result<List<String>> listRole() {
        return Result.SUCCESS(rolePermissionService.listRole());
    }

    @PostMapping("/insertPermission")
    @ApiOperation("添加权限")
    @RequiresRoles("admin")
    public Result<Integer> insertPermission(@RequestBody RolePermission rolePermission) {
        return Result.SUCCESS(rolePermissionService.insertSelective(rolePermission));
    }

    @PostMapping("/deletePermission")
    @ApiOperation("删除权限")
    @RequiresRoles("admin")
    public Result<Integer> deletePermission(@RequestBody RolePermission rolePermission) {
        return Result.SUCCESS(rolePermissionService.deleteByPrimaryKey(rolePermission.getId()));
    }

    @GetMapping("/listPermission")
    @ApiOperation("列出所有权限")
    public Result<List<RolePermission>> listPermission() {
        return Result.SUCCESS(rolePermissionService.listPermission());
    }


    @PostMapping("/updatePermission")
    @ApiOperation("更新权限")
    @RequiresRoles("admin")
    public Result<Integer> updatePermission(@RequestBody RolePermission rolePermission) {
        return Result.SUCCESS(rolePermissionService.updateByPrimaryKeySelective(rolePermission));
    }
}
